package em.program.imp;

import com.alibaba.fastjson.JSONObject;
import em.util.CommonTools;
import em.util.TermUtil;
import em.util.ConstUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2016-1-30.
 */
public class OtherData extends MsgApiImp{
    private Map<String,String> saveMap=new ConcurrentHashMap<String,String>();
    private static OtherData otherData=new OtherData();
    private static java.util.concurrent.locks.ReadWriteLock rwlock=new java.util.concurrent.locks.ReentrantReadWriteLock();
    private Object objLock=new Object();
    public static OtherData getInstance(){
        return otherData;
    }
    /**
     * 终端返回的数据
     * @param jsonObject
     */
    protected void setData(JSONObject jsonObject){
        String strKey=jsonObject.getString("TNUM")+jsonObject.getString("ACTION");
        if("10".equals(jsonObject.getString("ACTION"))){//下发命令时回馈数据
            strKey=strKey+getKey(jsonObject);
        }
        saveMap.put(strKey,jsonObject.toString());
        setCurrentStatus(jsonObject.getString("TNUM"),0);//设置当前状态为正常
    }

    /**
     * 取得数据
     * @param strTnum 终端编号
     * @param iActionKey 指定key
     * @param strAction 向终端发送数据
     * @param iSecond 有效时间内
     * @return
     */
    public String getData(String strTnum,Integer iActionKey,String strAction,Integer iSecond){
        //发送指令
        //判断是否要发送指令
        Integer iStatus= isCheckStatus(strTnum,iActionKey);
        if(iStatus!=0 && iStatus!=-2){
            return String.format(ConstUtil.ERROR_ACTION,iStatus);
        }else{
            sendData(strAction);//发送指令
            setCurrentStatus(strTnum,iActionKey);
        }
        String strKey=strTnum + iActionKey.toString();
        if(iActionKey==10){
            strKey=strKey+getKey(JSONObject.parseObject(strAction));
        }
        //取得数据
        return getResultData(strKey, saveMap, iSecond,true);
    }

    /**
     * 对于actioin10来说,里面会有很多小的命令,对于放到缓存里,对应的key就分不清是哪一种下发的命令,所以将其拆开
     * @param jsonObject
     * @return
     */
    private String getKey(JSONObject jsonObject){
        if(jsonObject.containsKey("EOPR")){//锁机
            return "EOPR";
        }else if(jsonObject.containsKey("SHIELD")){//IC卡(身份认证)
            return "SHIELD";
        }else if(jsonObject.containsKey("MTYPE")){//主要类型
            return "MTYPE";
        }else if(jsonObject.containsKey("LAUNCH")){//下发HLC命令
            return "LAUNCH";
        }
        return "";
    }

    /**
     * 绑定终端
     * @param strTnum
     * @param eType
     * @param eNum
     * @return
     */
    public String BindTerm(String strTnum, int eType, String eNum) {
        //发送命令
        sendData(String.format(ConstUtil.ACTION_2,strTnum,eType,eNum));
        setCurrentStatus(strTnum,2);
//        String strErr=getResultData(strTnum+"2", saveMap, 20,true);
//        JSONObject jsonObject= JSONObject.parseObject(strErr);
//        if(!jsonObject.containsKey("err")) jsonObject.put("err",0);
//        return jsonObject.toJSONString();
        return String.format(ConstUtil.ERROR_ACTION,0);
    }

    /**
     * 查询终端版本信息
     *
     * @param strTnum
     * @return
     */
    public String getTermVersion(String strTnum) {
       return getData(strTnum,5,String.format(ConstUtil.ACTION_5,strTnum),60);
    }

    /**
     * 发送升级版本数据
     *
     * @param iTnum
     * @param Updata
     * @return err:1,2,4,6,8
     *
     */
    public String sendTermUpdateData(Integer iTnum, String HVER,String NSVER, byte[] Updata,int FPOS,int SPKG) {
        int iLen,err;
        String iProg;
        iLen=Updata.length;
        if(FPOS+SPKG>iLen){//是否到了升级包结尾
            SPKG=iLen-FPOS;
        }
        iProg=CommonTools.getInstance().getTwoPoint ((((double)(FPOS+SPKG)) / (double) iLen) * 100);
        //判断是否可以升级
        Integer iStatus=isCheckStatus(iTnum.toString(),5);
        if(iStatus!=0 && iStatus!=-2) {
            return "{\"err\":6,\"PROG\":" + iProg + ",\"FPOS\":" + FPOS + "}";
        }

        //初始化升级包数据信息
        JSONObject comm = new JSONObject();
        comm.put("TNUM", iTnum);
        comm.put("ACTION", 5);
        comm.put("SOFT", "");
        comm.put("HVER", HVER);
        comm.put("NSVER", NSVER);
        comm.put("FPOS", FPOS);
        comm.put("SPKG", SPKG);

        sendData(comm.toString(), Updata, FPOS, SPKG);//发送升级包

        JSONObject json = JSONObject.parseObject(getResultData(iTnum+"5",saveMap,60,true));

        if(json.containsKey("SOFT")){
            err=json.getIntValue("SOFT");
            switch (json.getIntValue("SOFT")) {
                case 1:
                    FPOS=json.getIntValue("FPOS");
                    iProg = CommonTools.getInstance().getTwoPoint ((((double)(FPOS+SPKG)) / (double) iLen) * 100);
                    break;
                case 2:
                    iProg="100";
                    break;
                case 4:
                    break;
            }
        }else{
            err=json.getIntValue("err");
        }
        return "{\"err\":"+err+",\"PROG\":"+iProg+",\"FPOS\":"+FPOS+"}";
    }

    public String sendUpDataVersionInfo(Integer iTnum, String HVER,String NSVER, byte[] Updata){
        //判断是否可以升级
        Integer iStatus=isCheckStatus(iTnum.toString(),5);
        if(iStatus!=0 && iStatus!=-2) {
            return String.format(ConstUtil.ERROR_ACTION,6);
        }
        setCurrentStatus(iTnum.toString(),5);

        long CHECK = TermUtil.CRC32(Updata);
        int SLEN = Updata.length;
        JSONObject comm=new JSONObject();
        comm.put("TNUM", iTnum);
        comm.put("ACTION", 5);
        comm.put("SOFT", "");
        comm.put("HVER", HVER);
        comm.put("NSVER", NSVER);
        comm.put("SLEN", SLEN);
        comm.put("CHECK", CHECK);
        if(!sendData(comm.toString()))
            return String.format(ConstUtil.ERROR_ACTION,8);
        comm.clear();
        comm = JSONObject.parseObject(getResultData(iTnum+"5",saveMap,60,true));
        if (comm.containsKey("FPOS")) {
            return "{\"FPOS\":"+comm.getIntValue("FPOS")+"}";
        }else{
            return String.format(ConstUtil.ERROR_ACTION,8);
        }
    }
}
